package com.xidian.zhiancloud.controller.riskcontrol;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TRiskLedgerStatusList;
import com.xidian.zhiancloud.model.riskcontrol.entity.TRLedgerStaGY00Entity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TRLedgerStaSS00Entity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TRLedgerStaZY00Entity;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.riskcontrol.TRiskLedgerStatusServicve;
import com.xidian.zhiancloud.service.riskcontrol.riskidenva.TRiskAssessmentMeasuresService;
import com.xidian.zhiancloud.service.riskcontrol.riskpointslist.TEquipFacStatusService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险管控-风险台账
 */
@RestController
@RequestMapping("/trledsta")
public class TRiskLedgerStatusController extends BaseController {

    @Autowired
    TRiskLedgerStatusServicve tRiskLedgerStatusServicve;

    @Autowired
    TEquipFacStatusService tEquipFacStatusService;

    /**
     * 风险评价措施表
     */
    @Autowired
    TRiskAssessmentMeasuresService tRiskAssessmentMeasuresService;

    /**
     * 风险台账列表
     * @param reqdata 多个对象集合
     * @param riskPointType 风险点类型
     * @param locale 区域/位置
     * @param unit 责任单位
     * @param levelRisk 风险级别
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTRiskLedgerStatusList")
    public ResultDTO getTRiskLedgerStatusList(BaseReqQuery reqdata,
           @RequestParam(value = "riskType",required = false,defaultValue = "设备设施") 
               String riskType,
           @RequestParam(value = "locale",required = false) String locale,
           @RequestParam(value = "unit",required = false) String unit,
        @RequestParam(value = "levelRisk",required = false) String levelRisk){
        Long companyId=this.getSessionUserCompanyId();
        Map<String, Object> data = new HashMap<String, Object>();
        if(riskType.equals("设备设施")){//设备设施
            Page<TRLedgerStaSS00Entity> pagedata = tRiskLedgerStatusServicve
                .getLedgerStaSS00Data(reqdata,locale,unit,levelRisk,companyId);
            data.put("datalist", pagedata);
            data.put("PageNum", pagedata.getPageNum());
            data.put("PageSize", pagedata.getPageSize());
            data.put("PageTotal", pagedata.getPages());
            data.put("total", pagedata.getTotal());
        }else if(riskType.equals("作业活动")){//作业活动
            Page<TRLedgerStaZY00Entity> pagedata = tRiskLedgerStatusServicve
                    .getLedgerStaZY00Data(reqdata,locale,unit,levelRisk,companyId);
            data.put("datalist", pagedata);
            data.put("PageNum", pagedata.getPageNum());
            data.put("PageSize", pagedata.getPageSize());
            data.put("PageTotal", pagedata.getPages());
            data.put("total", pagedata.getTotal());
        }else if(riskType.equals("工艺节点")){//工艺节点
            Page<TRLedgerStaGY00Entity> pagedata = tRiskLedgerStatusServicve
                    .getLedgerStaGY00Data(reqdata,locale,unit,levelRisk,companyId);
            data.put("datalist", pagedata);
            data.put("PageNum", pagedata.getPageNum());
            data.put("PageSize", pagedata.getPageSize());
            data.put("PageTotal", pagedata.getPages());
            data.put("total", pagedata.getTotal());
        }
//        else if(riskPointType.equals("0")){//全部
//            Page<TRLedgerStaGY00Entity> pagedata = tRiskLedgerStatusServicve
//                .getLedgerStaData(reqdata,locale,unit,levelRisk,companyId);
//            data.put("datalist", pagedata);
//            data.put("PageNum", pagedata.getPageNum());
//            data.put("PageSize", pagedata.getPageSize());
//            data.put("PageTotal", pagedata.getPages());
//            data.put("total", pagedata.getTotal());
//        }
        return ResultDTO.successOf(data, GeneralResponseEnums.SUCCESS);
    }

    /**
     *  -导出风险台账列表
     * @param id 风险台账ID
     * @param riskPointType 风险台账类型
     * response
     *
     */
    @GetMapping("/exportLedgerStaData")
    public void exportLedgerStaData (HttpServletResponse response,
           @RequestParam("riskPointType") String riskPointType,
          @RequestParam(value="id",required=false ,defaultValue="0") String id)
            throws IOException {
        Long companyId=this.getSessionUserCompanyId();

        if(riskPointType.equals("ss00")){//设备设施
            Page<TRLedgerStaSS00Entity> pagedata = tRiskLedgerStatusServicve
                 .getExportLedgerStaSS00Data(companyId,id);
            List<TRLedgerStaSS00Entity> list = pagedata.getResult();
            ExcelUtil2<TRLedgerStaSS00Entity> util = new ExcelUtil2
                      <TRLedgerStaSS00Entity>(TRLedgerStaSS00Entity.class);
            util.exportExcel(response, list, "风险台账-设备设施");
        }else if(riskPointType.equals("zy00")){//作业活动
            Page<TRLedgerStaZY00Entity> pagedata = tRiskLedgerStatusServicve
                 .getExportLedgerStaZY00Data(companyId,id);
            List<TRLedgerStaZY00Entity> list = pagedata.getResult();
            ExcelUtil2<TRLedgerStaZY00Entity> util = new ExcelUtil2
                      <TRLedgerStaZY00Entity>(TRLedgerStaZY00Entity.class);
            util.exportExcel(response, list, "风险台账-作业活动");
        }else if(riskPointType.equals("gy00")){//工艺节点
            Page<TRLedgerStaGY00Entity> pagedata = tRiskLedgerStatusServicve
                  .getExportLedgerStaGY00Data(companyId,id);
            List<TRLedgerStaGY00Entity> list = pagedata.getResult();
            ExcelUtil2<TRLedgerStaGY00Entity> util = new ExcelUtil2
                      <TRLedgerStaGY00Entity>(TRLedgerStaGY00Entity.class);
            util.exportExcel(response, list, "风险台账-工艺节点");
        }

    }

    /**
     * 风险台账详情页面数据
     * @param id 风险台账ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getRiskLedgerDetails")
    public ResultDTO getRiskLedgerDetails(@RequestParam("id") String id){
        Long companyId=this.getSessionUserCompanyId();
        List<TRiskLedgerStatusList> ledgerDetList = tRiskLedgerStatusServicve
                .getRiskLedgerDetails(companyId,id);
        return ResultDTO.successOf(ledgerDetList,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 编辑风险台账信息
     * @param id 风险台账ID
     * @param riskPointsId 风险点ID
     * @param riskPicture 风险图片
     * @param riskAssmeaId 风险评价措施ID
     * @return 返回ResultDTO对象
     */
    @RequestMapping("/updateRiskLedgerDetails")
    public ResultDTO updateRiskLedgerDetails
        (@RequestParam("id") String id,
         @RequestParam("riskPointsId") String riskPointsId, String riskPicture,
         @RequestParam("riskAssmeaId") String riskAssmeaId){
        int res = tRiskLedgerStatusServicve.updateRiskLedgerDetails(id);
        if (res != 0){
            tEquipFacStatusService.updateIdAndRiskPicture(riskPointsId, riskPicture);
            tRiskAssessmentMeasuresService.updateIdAndAssessMeas(riskAssmeaId);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 查询重大风险
     * @return
     */
    @ApiOperation("查询重大风险")
    @RequestMapping("/getImportantRisk")
    public ResultDTO getImportantRisk () {
        String levelRisk ="重大";
        List<TRiskLedgerStatusList> data = tRiskLedgerStatusServicve.getImportantRisk(levelRisk);
        return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }
}
